/**
 * 
 */
package com.lijin.workshop.web.action;

import java.util.Collections;
import java.util.Map;

import com.lijin.workshop.constants.SysConstants;
import com.lijin.workshop.util.AssertUtil;
import com.lijin.workshop.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author jin
 *
 */
public abstract class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	protected static final String AJAX_RESULT_TYPE = "ajaxResult";
	protected static final String AJAX_RESULT_SUCC = "succ";
	protected static final String AJAX_RESULT_FAIL = "fail";
	
	protected static final String RESULT_MANAGE = "manage";
	
	protected String ajaxResult;
	
	protected int pageNum = 0;
	protected int pageSize = 10;
	
	/**
	 * 从session中获取查询参数MAP，如果找不到，则返回一个空MAP
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected Map<String, String> getQryParamMap() {
		Object o = this.getSession().get(getQryParamMapKey(SysConstants.QRY_PARAM_MAP_KEY));
		if (o != null) {
			return (Map<String, String>) o;
		} else {
			return Collections.emptyMap();
		}
	}
	
	/**
	 * 从ActionContext中获取新增参数MAP，如果找不到，则返回一个空MAP
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected Map<String, String> getInsertParamMap() {
		Object o = this.getContext().get(getQryParamMapKey(SysConstants.INSERT_PARAM_MAP_KEY));
		if (o != null) {
			return (Map<String, String>) o;
		} else {
			return Collections.emptyMap();
		}
	}
	
	/**
	 * 从ActionContext中获取修改参数MAP，如果找不到，则返回一个空MAP
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected Map<String, String> getUpdateParamMap() {
		Object o = this.getContext().get(getQryParamMapKey(SysConstants.UPDATE_PARAM_MAP_KEY));
		if (o != null) {
			return (Map<String, String>) o;
		} else {
			return Collections.emptyMap();
		}
	}
	
	/**
	 * 构造操作结果返回信息，格式为JSON串：
	 * {"result":"succ or fail", "msg":""}
	 * 用result表示成功还是失败，如果失败，将失败原因放在msg中，前台解析本JSON串可知道操作结果
	 * @param msg
	 * @return
	 */
	protected String buildJSONResp(String result, String msg) {
		AssertUtil.isArgumentBlank(result, "操作结果不能为空");
		StringBuilder sb = new StringBuilder();
		sb.append("{\"result\":\"").append(result).append("\"");
		if (!StringUtil.isBlank(msg)) {
			sb.append(",\"msg\":\"").append(msg).append("\"");
		}
		sb.append("}");
		return sb.toString();
	}
	
	protected Map<String, Object> getContext() {
		return ActionContext.getContext().getContextMap();
	}
	
	protected Map<String, Object> getSession() {
		return ActionContext.getContext().getSession();
	}
	
	private String getQryParamMapKey(String paramTp) {
		return this.getClass().getName() + paramTp;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public String getAjaxResult() {
		return ajaxResult;
	}
}

